package 链表.双向.基础;

import 链表.单向.基础.MyLinkList;

class ListNode{
    int val;
    ListNode prev;
    ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class MyLinkedList {
    ListNode head;
    ListNode last;
    public void addFirst(int data){
        ListNode newList  = new ListNode(data);
        ListNode cur = newList;
        if(head==null){
            head = cur;
            last = cur;
        }else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode newList = new ListNode(data);
        ListNode cur = newList;
        if(last ==null){
            last = cur;
            head = cur;
        }else {
            last.next = cur;
            cur.prev = last;
            last = cur;
        }

    }
 //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode newList = new ListNode(data);
        ListNode cur;
        ListNode node = newList;
        cur = head;
        if(index<0||index>size()){
            System.out.println("位置不对");
            return;
        }
        if(index ==0){
            addFirst(data);
            return;
        }
        if(index ==size()){
            addLast(data);
            return;
        }
        while(index>0){
            index--;
            cur = cur.next;
        }
        if(cur!=null){
            node.next = cur;
            node.prev = cur.prev;
            cur.prev.next = node;
            cur.prev = node;
        }
    }
  //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur!=null){
            if(cur.val ==key){
                return true;
            }
            cur =cur.next;
        }
        return false;
    }
 //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur!= null){
            if(cur.val ==key) {
                //头相同
                if(cur == head){
                    head= head.next;
                    //只有一个节点
                    if(head!=null){
                        head.prev = null;
                    }else {
                        last = null;
                    }

                }else {
                    //中间相同
                    if(cur.next!= null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {   //尾巴相同
                        last.prev.next = null;
                        last =last.prev;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }
  //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur!= null){
            if(cur.val ==key) {
                //头相同
                if(head == cur){
                    head= head.next;
                    //只有一个节点
                    if(head!=null){
                        head.prev = null;
                    }else {
                        last = null;
                    }

                }else {
                    //中间相同
                    if(cur.next!= null){
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }else {   //尾巴相同
                        last.prev.next = null;
                        last =last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
  //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int count =  0;
        while (cur!=null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next =null;
            cur.prev = null;
            cur = curNext;
        }
        head = null;
        last= null;
    }

    public static void main(String[] args) {
        //头插
        MyLinkedList myLinkList = new MyLinkedList();
        myLinkList.addFirst(1);
        myLinkList.addFirst(2);
        myLinkList.addFirst(3);
        myLinkList.display();

        System.out.println();

        //尾插
        MyLinkedList myLinkList1 = new MyLinkedList();
        myLinkList1.addLast(1);
        myLinkList1.addLast(2);
        myLinkList1.addLast(3);
        myLinkList1.addFirst(3);
        myLinkList1.display();

        System.out.println();

        //固定位置插入
        myLinkList1.addIndex(2,33);
        myLinkList1.display();

        System.out.println("========");

        //删除第一个
        myLinkList1.remove(3);
        myLinkList1.display();

        System.out.println();
        //删除所有
        myLinkList1.addFirst(55);
        myLinkList1.addLast(55);
        myLinkList1.display();
        System.out.println();
        myLinkList1.removeAllKey(55);
        myLinkList1.display();


    }
}
